package com.hx0311.core.webapp.struts2.action;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;

import com.hx0311.core.ApplicationException;
import com.hx0311.core.dao.support.Page;
import com.hx0311.core.util.GenericsUtil;
import com.hx0311.core.util.ReflectUtil;
import com.hx0311.core.webapp.struts2.CrudAction;
import com.hx0311.pas.PasConstants;
import com.hx0311.pas.security.user.model.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings( { "serial", "unchecked" })
public abstract class BaseAction<T, D> extends ActionSupport implements
		ModelDriven<T>, CrudAction<D> {

	public final static String ADD = "ADD";

	/** 操作类型[增加,更新] */
	public String optionType;

	/** Action所管理的Entity */
	private T model;

	/** Action所管理的实体的类型 */
	private Class entityClass;

	/** 用于保存查询结果 */
	protected Collection items;

	/** 用于保存分页查询结果 */
	protected Page page;

	/** struts2的ActionContext */
	private ActionContext actionContext;

	protected static final String INDEX = "index";

	public String getOptionType() {
		return optionType;
	}

	public void setOptionType(String optionType) {
		this.optionType = optionType;
	}

	@Override
	public T getModel() {
		if (model == null) {
			model = (T) ReflectUtil.newInstance(getEntityClass());
		}
		return model;
	}

	public void setModel(T model) {
		this.model = model;
	}

	public Collection getItems() {
		return items;
	}

	public void setItems(Collection items) {
		this.items = items;
	}

	public void setEntityClass(Class entityClass) {
		this.entityClass = entityClass;
	}

	private Class getEntityClass() {
		if (entityClass == null) {
			entityClass = GenericsUtil.getGenericClass(getClass(), 0);
		}
		return entityClass;
	}

	protected final ActionContext getActionContext() {
		return (actionContext == null) ? actionContext = ActionContext
				.getContext() : actionContext;
	}

	protected final HttpServletRequest getRequest() {
		HttpServletRequest request = null;
		if (getActionContext() != null) { // 从Struts2中取得Request
			request = (HttpServletRequest) getActionContext().get(
					StrutsStatics.HTTP_REQUEST);
		}
		return request;
	}

	protected final HttpServletResponse getResponse() {
		HttpServletResponse response = null;
		if (getActionContext() != null) { // 从Struts2中取得Response
			response = (HttpServletResponse) getActionContext().get(
					StrutsStatics.HTTP_RESPONSE);
		}
		return response;
	}

	protected final ServletContext getServletContext() {
		ServletContext ctx = (ServletContext) getActionContext().get(
				StrutsStatics.SERVLET_CONTEXT);

		return ctx;
	}

	/**
	 * 直接输出数据，而不是通过struts重新定向.
	 * 
	 * @param response
	 * @param text
	 * @param contentType
	 */
	protected void render(HttpServletResponse response, String text,
			String contentType) {
		try {
			response.setContentType(contentType);
			response.getWriter().write(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 直接输出Json.
	 */
	protected void renderJson(HttpServletResponse response, String text) {
		render(response, text, "text/x-json;charset=UTF-8");
	}

	/**
	 * 直接输出纯XML.
	 */
	protected void renderXml(HttpServletResponse response, String text) {
		render(response, text, "text/xml;charset=UTF-8");
	}

	public User getLoginUser() {
		User u = (User) getRequest().getSession().getAttribute(PasConstants.USER_IN_SESSION);
		if (u == null){
			new ApplicationException("未登录,请重新登录系统");
		}
		return u;
	}

}
